package com.zr.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Distributionrangeprice;
import com.zr.model.User;
import com.zr.service.DistributionRangePriceServiceLPN;

import net.sf.json.JSONObject;
/**
 * 配送间范围价格
 * @author 李培宁
 *
 */
public class DistributionRangePriceActionLPN extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{

	private HttpServletRequest req;
	private HttpServletResponse resp;
	private Map<String, Object> session;
	private int page;
	private int rows;
	private int drpId;
	private Double drpFirstKilogramPrice;
	private Double drpNextKilogramPrice;
	private Double drpFirstCubicPrice;
	private Double drpNextCubicPrice;
	private String drpRemarks;
	private int dsId;
	private int otId;
	private String drpIds;
	



	public String getDrpIds() {
		return drpIds;
	}


	public void setDrpIds(String drpIds) {
		this.drpIds = drpIds;
	}


	public int getDrpId() {
		return drpId;
	}


	public void setDrpId(int drpId) {
		this.drpId = drpId;
	}


	public Double getDrpFirstKilogramPrice() {
		return drpFirstKilogramPrice;
	}


	public void setDrpFirstKilogramPrice(Double drpFirstKilogramPrice) {
		this.drpFirstKilogramPrice = drpFirstKilogramPrice;
	}




	public Double getDrpNextKilogramPrice() {
		return drpNextKilogramPrice;
	}


	public void setDrpNextKilogramPrice(Double drpNextKilogramPrice) {
		this.drpNextKilogramPrice = drpNextKilogramPrice;
	}


	public Double getDrpFirstCubicPrice() {
		return drpFirstCubicPrice;
	}


	public void setDrpFirstCubicPrice(Double drpFirstCubicPrice) {
		this.drpFirstCubicPrice = drpFirstCubicPrice;
	}


	public Double getDrpNextCubicPrice() {
		return drpNextCubicPrice;
	}


	public void setDrpNextCubicPrice(Double drpNextCubicPrice) {
		this.drpNextCubicPrice = drpNextCubicPrice;
	}


	public String getDrpRemarks() {
		return drpRemarks;
	}


	public void setDrpRemarks(String drpRemarks) {
		this.drpRemarks = drpRemarks;
	}


	public int getDsId() {
		return dsId;
	}


	public void setDsId(int dsId) {
		this.dsId = dsId;
	}


	public int getOtId() {
		return otId;
	}


	public void setOtId(int otId) {
		this.otId = otId;
	}


	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public int getRows() {
		return rows;
	}


	public void setRows(int rows) {
		this.rows = rows;
	}

	//属性注入
		@Resource
		private DistributionRangePriceServiceLPN drpservice;
	/**
	 * 得到配送间价格范围表
	 * @throws IOException 
	 */
	public void getDistributionRangePrice() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		User u=(User) session.get("user");
		JSONObject j = drpservice.getDistributionRangePrice(page, rows,u.getUid());
		PrintWriter pw = resp.getWriter();
		pw.write(j.toString());
		pw.flush();
		pw.close();
	}
	/**
	 * 增加配送范围价格
	 * @throws IOException 
	 */
	public void insertDRP() throws IOException{		
		boolean flag = drpservice.insertDRP(drpFirstKilogramPrice, drpNextKilogramPrice, drpFirstCubicPrice, drpNextCubicPrice, drpRemarks, dsId, otId);
		PrintWriter pw = resp.getWriter();
		if (flag) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	/**
	 * 增加时验证是否已存在
	 * @throws IOException
	 */
	public void validateAddDrp() throws IOException{
		int i = drpservice.finddrpIdBydsIdAndotId(dsId, otId);
		PrintWriter pw = resp.getWriter();
		if (i>0) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	/**
	 * 修改配送范围价格
	 * @throws IOException 
	 */
	public void updateDRP() throws IOException{
		int i = drpservice.updateDRP(drpId, drpFirstKilogramPrice, drpNextKilogramPrice, drpFirstCubicPrice, drpNextCubicPrice, drpRemarks);
		PrintWriter  pw = resp.getWriter();
		if (i>0) {
			pw.write("1");
		} else {
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
	
	/**
	 * 删除配送范围价格
	 * @throws IOException 
	 */
	public void deleteDRP() throws IOException{
		PrintWriter  pw = resp.getWriter();
		String[] drpIdsArr = drpIds.split(",");
		int j = 0;
		for (int i = 0; i < drpIdsArr.length; i++) {
			int drpid = Integer.parseInt(drpIdsArr[i]);
			 j = drpservice.deleteDRP(drpid);		
		}
		if (j>0) {
			pw.write("1");
		} else {
			pw.write("0");
		}
	}
	
	
	

	
	
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req=arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session=arg0;
	}

}
